package com.doaio.controller.admin;

import com.doaio.constant.ErrorConstant;
import com.doaio.controller.BaseController;
import com.doaio.exception.BusinessException;
import com.doaio.model.LinkDomain;
import com.doaio.service.link.LinkService;
import com.doaio.utils.APIResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 雷楚桥
 * @deprecation 友链控制器
 * @package com.doaio.controller.admin
 * @email leichuqiao@126.com
 * @date 2018/11/22 16:49
 */
@Api("友链")
@Controller
@RequestMapping(value = "admin/links")
public class LinksController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(LinksController.class);

    @Autowired
    private LinkService linkService;

    @ApiOperation("友链页面")
    @GetMapping(value = "")
    public String index(HttpServletRequest request) {

        List<LinkDomain> links = linkService.getLinks();
        request.setAttribute("links", links);
        return "admin/links";
    }

    @ApiOperation("保存友链")
    @PostMapping(value = "save")
    @ResponseBody
    public APIResponse addLink(
            @ApiParam(name = "name", value = "姓名", required = true)
            @RequestParam(name = "name", required = true)
                    String name,
            @ApiParam(name = "home", value = "主页", required = true)
            @RequestParam(name = "home", required = true)
                    String home,
            @ApiParam(name = "imgUrl", value = "展示图片", required = false)
            @RequestParam(name = "imgUrl", required = false)
                    String imgUrl,
            @ApiParam(name = "lid", value = "标识", required = false)
            @RequestParam(name = "lid", required = false)
                    Integer lid,
            @ApiParam(name = "description", value = "描述", required = false)
            @RequestParam(name = "description", required = false)
                    String description,
            @ApiParam(name = "sort", value = "sort", required = false)
            @RequestParam(name = "sort", required = false, defaultValue = "0")
                    int sort
    ) {
        try {
            LinkDomain link = new LinkDomain();
            link.setName(name);
            link.setHome(home);
            link.setImgUrl(imgUrl);
            link.setDescription(description);
            link.setSort(sort);

            if (null != lid) {
                link.setLid(lid);
                linkService.updateLink(link);
            } else {
                linkService.addLink(link);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw BusinessException.withErrorCode(ErrorConstant.Link.ADD_Link_FAIL);
        }
        return APIResponse.success();
    }


    @ApiOperation("删除友链")
    @PostMapping(value = "delete")
    @ResponseBody
    public APIResponse delete(
            @ApiParam(name = "lid", value = "link主键", required = true)
            @RequestParam(name = "lid", required = true) int lid) {
        try {
            linkService.deleteLinkById(lid);
        } catch (Exception e) {
            e.printStackTrace();
            throw BusinessException.withErrorCode(ErrorConstant.Link.DELETE_Link_FAIL);
        }
        return APIResponse.success();

    }
}
